package vn.lumi.smarthome.network;

import vn.lumi.smarthome.R;
import vn.lumi.smarthome.io.database.TBL_Device;
import vn.lumi.smarthome.io.database.TBL_DeviceState;
import vn.lumi.smarthome.io.model.Device;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.os.Parcel;

public class ControlRGB extends AppMsg{

	//Attributes
	private int NumOfDevice = 0;
	private int[] deviceIDList = null;
	private int[] deviceStateList = null;
	private int[] deviceStateIDList = null;
	
	public ControlRGB(){
		super(NetMessage.CMD_APP_CONTROL_RGB);
	}
	
	public ControlRGB(int Msg_id, int uid, int sid, int[] DeviceIDList, int[] DeviceStateList, int[] DeviceStateIDList) {
		super(NetMessage.CMD_APP_CONTROL_RGB, Msg_id, uid, sid);
		// TODO Auto-generated constructor stub
		NumOfDevice = DeviceIDList.length;
		deviceIDList = new int[NumOfDevice];
		deviceStateList = new int[3*NumOfDevice];
		deviceStateIDList = new int[NumOfDevice];
		for(int i = 0; i< NumOfDevice; i++){
			deviceIDList[i] = (byte) DeviceIDList[i];
			deviceStateList[3*i] =  DeviceStateList[0];
			deviceStateList[3*i +1] =  DeviceStateList[1];
			deviceStateList[3*i +2] =  DeviceStateList[2];
			deviceStateIDList[i] = DeviceStateIDList[i];
		}
		
	}

	@Override
	public byte[] generateByteArray() {
		// TODO Auto-generated method stub

		byte[] result = new byte[5+4*NumOfDevice];
		result[0] = (byte) this.Cmd_id;
		result[1] = (byte) this.Msg_id;
		result[2] = (byte) this.user_id;
		result[3] = (byte) this.session_id;
		result[4] = (byte) this.NumOfDevice;
		for(int i = 0; i<NumOfDevice;i++){
			result[5+4*i] = (byte) deviceIDList[i];
			result[6+4*i] = (byte) deviceStateList[3*i + 0];
			result[7+4*i] = (byte) deviceStateList[3*i + 1];
			result[8+4*i] = (byte) deviceStateList[3*i + 2];
		}
		return result;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.writeInt(this.Cmd_id);
		dest.writeInt(this.length);
		dest.writeInt(this.Msg_id);
		dest.writeInt(this.NumOfDevice);
		dest.writeInt(this.session_id);
		dest.writeInt(this.user_id);
		dest.writeIntArray(deviceIDList);
		dest.writeIntArray(deviceStateList);		
		dest.writeIntArray(this.deviceStateIDList);
		
	}

	@Override
	public void doIfSendSuccessful(Context context) {
		// TODO Auto-generated method stub
		for(int i = 0; i< NumOfDevice; i++){
			//update device state
			ContentValues values = new ContentValues();
			values.clear();
			values.put(TBL_DeviceState.ID, String.valueOf(deviceStateIDList[i]));
			int rgb = deviceStateList[3*i] * 256*256 + deviceStateList[3*i+1] *256 + deviceStateList[3*i+2];
			values.put(TBL_DeviceState.RGB, rgb);
			String selection = TBL_DeviceState.ID+"=?";
			String[] selectionArgs = new String[]{String.valueOf(deviceStateIDList[i])+""};
			context.getContentResolver().update(TBL_DeviceState.CONTENT_URI_DEVICE_STATE, values, selection, selectionArgs);
			
			//clear isControl in device table
			values.clear();
			values.put(TBL_Device._ID, deviceIDList[i]);
			values.put(TBL_Device.IS_CONTROL, Device.DEVICE_STATE_CONTROL_NO);
			selection = TBL_Device._ID+"=?";
			selectionArgs = new String[]{String.valueOf(deviceIDList[i])+""};
			context.getContentResolver().update(TBL_Device.CONTENT_URI_DEVICES, values, selection, selectionArgs);
			//Hieu them vao
			//inform UI
		    Intent intent = new Intent();
		    intent.setAction(context.getString(R.string.txt_broadcast_device_control_successful));
		    context.sendBroadcast(intent);
		}
		
	}

	@Override
	public void doIfSendFail(Context context) {
		// TODO Auto-generated method stub
		for(int i = 0; i< NumOfDevice; i++){
			ContentValues values = new ContentValues();
			values.put(TBL_Device._ID, deviceIDList[i]);
			values.put(TBL_Device.IS_CONTROL, Device.DEVICE_STATE_CONTROL_NO); 
			String selection = TBL_Device._ID+"=?";
			String[] selectionArgs = new String[]{deviceIDList[i]+""};
			context.getContentResolver().update(TBL_Device.CONTENT_URI_DEVICES, values, selection, selectionArgs);
			
			
		}
		// Hieu them vao
					//inform UI
				    Intent intent = new Intent();
				    intent.setAction(context.getString(R.string.txt_broadcast_device_control_failed));
				    context.sendBroadcast(intent);
	}

	@Override
	public void readFromParcel(Parcel in) {
		// TODO Auto-generated method stub
		Cmd_id = in.readInt();
		length = in.readInt();
		Msg_id = in.readInt();
		NumOfDevice = in.readInt();
		session_id = in.readInt();
		user_id = in.readInt();
		in.readIntArray(deviceIDList);
		in.readIntArray(deviceStateList);
		in.readIntArray(deviceStateIDList);
		
	}

	@Override
	public boolean isCorrectReply(byte[] input, int length) {
		// TODO Auto-generated method stub
		boolean result = false;
		HC_ACK ack = new HC_ACK();
		if(ack.isCorrectFormat(input, length)){
			ack.parseMessage(input, length);
			if(ack.getMsg_id() == this.Msg_id) result = true;
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#isCorrectBusy(byte[], int)
	 */
	@Override
	public boolean isCorrectBusy(byte[] input, int length) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#doIfHcBusying()
	 */
	@Override
	public void doIfHcBusying() {
		// TODO Auto-generated method stub
		
	}

}
